const initialState = {
  list: [
    { id: 1, isDone: false, task: 'react' },
    { id: 2, isDone: true, task: 'ts' },
    { id: 3, isDone: false, task: 'vue3' },
  ],
  type: 'all',
};

export type ActionType =
  | { type: 'todo/delById'; payload: number }
  // 1. ActionType的类型
  | { type: 'todo/clearDone' };

const todoReducer = (state = initialState, action: ActionType): typeof initialState => {
  switch (action.type) {
    case 'todo/delById':
      return {
        ...state,
        list: state.list.filter((item) => {
          return item.id !== action.payload;
        }),
      };

    case 'todo/clearDone':
      return {
        ...state,
        list: state.list.filter((item) => !item.isDone),
      };
    default:
      return state;
  }
};

export default todoReducer;
